clear all;
close all;
clc;
input= rand(1,1000);           % randi() for bit generation
output=rand(1,1000);
for i=1:1000,   
    if(input(i)<0.5)
        input(i)=0;
    else
        input(i)=1;
    end
end

% Simple Trellis Encoder

trellis = struct('numInputSymbols',2,'numOutputSymbols',4,...
'numStates',4,'nextStates',[0 2;0 2;1 3;1 3],...
'outputs',[0 3;1 2;3 0;2 1]);

initial_state=0;

for i=1:1000
   
   next_state=trellis.nextStates(initial_state+1,input(i)+1);
   
   output(i)=trellis.outputs(initial_state+1,input(i)+1);
   initial_state=next_state;
end

 % S=dec2bin(output)

  % Generating 4-PPM wave from the output S
  P=4;      % PPM wave size
  Q=1000;  % Lenght of output with integer values
  
  % PPM Modulator
  
  a=zeros(1,P*Q)
  for i=1:Q
      for j=0:P-1
          if j==output(i)
              a((i-1)*P+j+1)=1;
          end
      end
  end
   
  % Lognormal channel Response
  
variance=.001
sigma=sqrt(variance)  

channel= exp(randn(size(a),1).*sigma)   

b = channel.*real(a); 

% PPM Demodulator
for i=1:1000,
    for j=0:3,
    if (b(i+j)>0.5)
        c(i)=j;
    end
end
end

% Viterbi Decoder

for i=1:1000,
    if(c(i)==0)
        d(i)=0;
    elseif (c(i)==1)
        d(i)=0;
    elseif (c(i)==2)
        d(i)=1;
    else
        d(i)=1;
    end
end

% BER calculation 
error=0;

for i=1:1000,
 error=error+xor(d(i),input(i));
end
  
BER=error/1000

         
 % Hello Guyz. I am testing the Commit Process 
 % Thank You. :-)
  